#include "StdAfx.h"
#include "FMConnection.h"

FMDBConnection::FMDBConnection(FMDBApplication* pOracleDB)
{
	m_pOracleDB=pOracleDB;
	m_pSvcCtx=NULL;
	m_stmtCache=-1;
	m_pDiagValues=NULL;
	m_pDiagCodes=NULL;
	m_iDiagNum=0;
	m_ConnectionStatus=csClosed;
	
	if (pOracleDB)
	{
		m_pOCIErr=pOracleDB->GetOCIErrorHandle();
	}
	else
	{
		m_pOCIErr=nullptr;
	}
}

FMDBConnection::FMDBConnection( void )
{
	FMDBConnection(NULL);
}
FMDBConnection::~FMDBConnection(void)
{
	if (m_pOCIErr)
	{
		OCIHandleFree(m_pOCIErr,OCI_HTYPE_ERROR);	//free error handle
	}
}

//caller should decide if open a new connection before close the old one.
void FMDBConnection::Open()
{
	//ASSERT(m_ConnectionStatus!=csOpened);//we recommended close the old connection before open a new one

	m_pSvcCtx=m_pOracleDB->CreateSession(m_pDiagValues,m_pDiagCodes,m_iDiagNum,m_stmtCache);
	if (m_pSvcCtx)
	{
		m_ConnectionStatus=csOpened;
	}
}

void FMDBConnection::Close()
{
	m_pOracleDB->ReleaseSession(m_pSvcCtx);
	m_ConnectionStatus=csClosed;
}

void FMDBConnection::GetDBServerVersion(text* buff,ub4 BuffSize)
{	
	OCIServerVersion (m_pSvcCtx,m_pOCIErr,buff,BuffSize,OCI_HTYPE_SVCCTX);
}

void FMDBConnection::GetDBClientVersion(sword *verMajor,sword *verMinor,sword *verUpdate,sword *verPatch,sword *verPort)
{
	OCIClientVersion(verMajor,verMinor,verUpdate,verPatch,verPort);
}

void FMDBConnection::SetDiagnosticAttributes( TCHAR (*AttriValue)[64],ub4 *AttriCode,short AttriNumber )
{
	m_pDiagValues=AttriValue;
	m_pDiagCodes=AttriCode;
	m_iDiagNum=AttriNumber;
}
